package com.zf.zfed.audit.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.zf.sqlite.utils.BlacklistEntity
import com.zf.zfed.audit.R

class BlacklistAdapter(
    private var blacklistItems: List<BlacklistEntity>,
    private val onRemoveClickListener: (BlacklistEntity) -> Unit
) : RecyclerView.Adapter<BlacklistAdapter.BlacklistViewHolder>() {

    class BlacklistViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val ivTypeIcon: ImageView = itemView.findViewById(R.id.iv_type_icon)
        val tvType: TextView = itemView.findViewById(R.id.tv_type)
        val tvDeviceName: TextView = itemView.findViewById(R.id.tv_device_name)
        val tvDeviceMac: TextView = itemView.findViewById(R.id.tv_device_mac)
        val tvDeviceManufacturer: TextView = itemView.findViewById(R.id.tv_device_manufacturer)
        val tvTime: TextView = itemView.findViewById(R.id.tv_time)
        val tvRemarks: TextView = itemView.findViewById(R.id.tv_remarks)
        val btnRemove: Button = itemView.findViewById(R.id.btn_remove)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BlacklistViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_blacklist, parent, false)
        return BlacklistViewHolder(view)
    }

    override fun onBindViewHolder(holder: BlacklistViewHolder, position: Int) {
        val item = blacklistItems[position]
        
        // 设置类型图标和文本
        holder.ivTypeIcon.setImageResource(item.typeIconResId)
        holder.tvType.text = item.typeDisplayName
        
        // 设置设备信息
        holder.tvDeviceName.text = item.device_name ?: "未知设备"
        holder.tvDeviceMac.text = item.device_mac ?: ""
        holder.tvDeviceManufacturer.text = item.device_manufacturer ?: ""
        
        // 设置时间
        holder.tvTime.text = item.backlist_add_time ?: ""
        
        // 设置备注
        holder.tvRemarks.text = item.remarks ?: ""
        
        // 设置移除按钮点击事件
        holder.btnRemove.setOnClickListener {
            onRemoveClickListener(item)
        }
    }

    override fun getItemCount(): Int = blacklistItems.size

    fun updateData(newItems: List<BlacklistEntity>) {
        blacklistItems = newItems
        notifyDataSetChanged()
    }
}
